package com.xdf.teacher.activity;

import android.content.Intent;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xdf.teacher.adapter.HomeListAdapter;
import com.xdf.teacher.appliction.MyApplication;
import com.xdf.teacher.entity.RoomList;
import com.xdf.teacher.entity.User;
import com.xdf.teacher.event.EventBuss;
import com.xdf.teacher.http.NetUtils;
import com.xdf.teacher.utils.Tools;

import net.neiquan.applibrary.base.BaseListActivity;
import net.neiquan.applibrary.base.MyBaseAdapter;
import net.neiquan.okhttp.NetCallBack;
import net.neiquan.okhttp.ResultModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.haitao.common.utils.ToastUtil;

import java.util.List;

/**
 * Created by JavaDog on 2018-3-21.
 */

public class HomeActivity extends BaseListActivity {
    @Override
    protected boolean isHaveHead() {
        return true;
    }

    @Override
    protected void setHeadViews() {
        EventBus.getDefault().register(this);
        setBackGone();
        setTitleTv("新东方双师");
    }

    /**
     * 普通事件
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN, priority = 100)
    public void onDataSynEvent(EventBuss event) {
        if (event.getState() == EventBuss.EXIT_ROOM) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected MyBaseAdapter getAdapter() {
        return new HomeListAdapter(this, null);
    }

    @Override
    protected int getLineNum() {
        return 0;
    }

    @Override
    public void initData() {
        adapter.setOnItemClickListener(new MyBaseAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, List data, int position) {
                RoomList.DataBean dataBean = (RoomList.DataBean) data.get(position);
                Intent intent = new Intent(HomeActivity.this, ChatRoomActivity.class);
                intent.putExtra("dataBean",dataBean);
                startActivity(intent);
            }
        });
        loadDatas();
    }

    private void loadDatas() {
        Tools.dismissWaitDialog();
        User user = MyApplication.getInstance().user;
        User.ResultBean result = user.getResult();
        NetUtils.getInstance().roomList(result.getId(), new NetCallBack() {
            @Override
            public void onSuccess(final String response, String mssage, ResultModel model) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Tools.dismissWaitDialog();
                        RoomList roomList = (RoomList) JSON.parseObject(response, RoomList.class);
                        List<RoomList.DataBean> list = roomList.getData();
                        if (mRecyclerview != null && mRefeshLy != null && adapter != null) {
                            mRecyclerview.loadMoreComplete();
                            mRecyclerview.refreshComplete();
                            mRefeshLy.hideAll();
                            if (roomList.getErrorcode() == -3) {
                                Tools.dismissWaitDialog();
                                ToastUtil.shortShowToast(roomList.getErrormsg());
                                mRefeshLy.showFailView();
                            } else {
                                if (pageNum == 0) {
                                    adapter.clear();
                                }
                                adapter.append(list);
                                if (list == null || list.size() < PAEG_SIZE) {
                                    if (pageNum == 0 && (list == null || list.size() == 0)) {
                                        mRefeshLy.showEmptyView();
                                    }
                                    mRecyclerview.setLoadingMoreEnabled(false);
                                } else {
                                    mRecyclerview.setLoadingMoreEnabled(false);
                                }
                            }
                        }
                    }
                });
            }

            @Override
            public void onFail(boolean dataError, int errorCode, String mssage) {

            }
        }, null);
    }


    @Override
    public void onRetryClick() {
        pageNum = 0;
        loadDatas();
    }

    @Override
    public void onRefresh() {
        pageNum = 0;
        loadDatas();
    }

    @Override
    public void onLoadMore() {
//        pageNum++;
//        loadDatas();
        mRecyclerview.loadMoreComplete();
        mRecyclerview.refreshComplete();
        mRefeshLy.hideAll();
    }
}
